#include "mainwindow.h"
#include "ui_mainwindow.h"

#include <QDragEnterEvent>
#include <QTextStream>
#include <QDropEvent>
#include <QMimeData>
#include <QIODevice>
#include <QFile>
#include <QUrl>
#include <QDebug>


MainWindow::MainWindow(QWidget *parent)
    : QMainWindow(parent)
    , ui(new Ui::MainWindow)
{
    ui->setupUi(this);
    this->setAcceptDrops(true);

    ui->textEdit->setAcceptDrops(false);
    qDebug() << "111";
}

MainWindow::~MainWindow()
{
    delete ui;
}

void MainWindow::dragEnterEvent(QDragEnterEvent* event)
{
    qDebug() << "111";
    if (event->mimeData()->hasUrls())
    {
        event->acceptProposedAction();
        qDebug() << "dragEnterEvent";
    }
    //qDebug() << "2222";
}


void MainWindow::dropEvent(QDropEvent *event)
{
    qDebug() << "3333";
    const QMimeData* mimeData = event->mimeData();
    if (mimeData->hasUrls())
    {
        QList<QUrl> urlList = mimeData->urls();
        QString fileName = urlList.at(0).toLocalFile();
        if (!fileName.isEmpty())
        {
            QFile file(fileName);
            if (!file.open(QIODevice::ReadOnly))    return;
            qDebug() << fileName;


            QTextStream in(&file);
            ui->textEdit->setText(in.readAll());

            //qDebug() << in.readAll();
        }
    }
}

void MainWindow::dragMoveEvent(QDragMoveEvent *event)
{
    //qDebug() << "12312";
}

